##########################################################################################

library(dplyr)
library(ggplot2)
library(data.table)
library(RColorBrewer)
library(optparse)
library(ggpubr)

##########################################################################################

option_list <- list(
    make_option(c("--gene"), type = "character") ,
    make_option(c("--info_file"), type = "character") ,
    make_option(c("--ccf_file"), type = "character") ,
    make_option(c("--images_path"), type = "character")
)

if(1!=1){
    
    gene <- "TP53"
    work_dir <- "~/20220915_gastric_multiple/dna_combine/"
    info_file <- paste(work_dir,"/config/STAD-useCombine.Sample.tsv",sep="")
    ccf_file <- paste(work_dir,"/mutationTime/result/All_CCF_mutTime.tsv",sep="")
	images_path <- paste(work_dir,"/images/mutCCF",sep="")

}

###########################################################################################

parseobj <- OptionParser(option_list=option_list, usage = "usage: Rscript %prog [options]")
opt <- parse_args(parseobj)
print(opt)

gene <- opt$gene
ccf_file <- opt$ccf_file
info_file <- opt$info_file
images_path <- opt$images_path

dir.create(images_path , recursive = T)

###########################################################################################

col <- c(
  brewer.pal(9,"YlGnBu")[6],
  rgb(234,106,79,alpha=255,maxColorValue=255),
  rgb(203,24,30,alpha=255,maxColorValue=255),
  rgb(255,0,0,alpha=255,maxColorValue=255)
  )

names(col) <- c("IM" , "IGC" , "DGC" , "GC")

Variant_Types <- c("Missense_Mutation","Nonsense_Mutation","Frame_Shift_Ins","Frame_Shift_Del","In_Frame_Ins","In_Frame_Del","Splice_Site","Nonstop_Mutation")

###########################################################################################

dat_sample <- data.frame(fread( info_file ))
dat_ccf <- data.frame(fread( ccf_file ))

###########################################################################################

dat_ccf <- subset( dat_ccf , Variant_Classification %in% Variant_Types & Hugo_Symbol == gene )
dat_sample_use <- dat_sample[,c("Patient" , "Tumor" , "Class" , "Class_sub" , "Type" )]
dat_ccf_use <- merge( dat_sample_use , dat_ccf , by.x = "Tumor" , by.y = "Sample" )

###########################################################################################
## 多个样本负荷取中位数
dat_plot2 <- dat_ccf_use %>%
group_by( Patient , Class , Type ) %>%
summarize( CCF = median(CCF_adj) )

dat_plot4 <- dat_plot2
dat_plot4$Type <- "All"

dat_plot_final <- dat_plot4
dat_plot_final$Class <- factor( dat_plot_final$Class , levels = names(col) , order = T )

###########################################################################################
plotBurden <- function(dat_plot_tmp = dat_plot_tmp , mutType = mutType){

    my_comparisons_1 <- list( 
    c(1, 2) , c(1, 3) ,
    c(2, 3)
    )

    plot <- ggplot( dat_plot_tmp , aes( x = Class , y = CCF , color = Class ) ) +
        geom_line( aes( group = Patient ) , size = 0.4 , color = "gray" ) +  ## 配对样本加线
        geom_boxplot(alpha =1 , outlier.size=0 , size = 0.9 , width = 0.6) +
        geom_jitter(position = position_jitter(0.17) , size = 1 , alpha = 0.7) +
        scale_color_manual(values=col) +
        xlab(NULL) +
        scale_y_continuous(trans="sqrt" , breaks = c(0,0.5,1,2,3,4,5,10,20,30,40,50) ) +
        ylab("Cell Fraction")+
        theme_bw() +
        stat_compare_means(comparisons = my_comparisons_1) +
        theme(panel.background = element_blank(),#设置背影为白色#清除网格线
            legend.position ='left',
            legend.title = element_blank() ,
            panel.grid.major=element_line(colour=NA),
            plot.title = element_text(size = 8,color="black",face='bold'),
            legend.text = element_text(size = 10,color="black",face='bold'),
            axis.text.y = element_text(size = 10,color="black",face='bold'),
            axis.title.x = element_text(size = 10,color="black",face='bold'),
            axis.title.y = element_text(size = 10,color="black",face='bold'),
            axis.ticks.x = element_blank(),
            axis.text.x = element_text(size = 10,color="black",face='bold') ,
            axis.line = element_line(size = 0.5)) 
    
    out_name <- paste0( images_path , "/mutCCF.",gene,".pdf" )  
    ggsave(file=out_name,plot=plot,width=4,height=5)
}

dat_plot_tmp <- subset( dat_plot_final , Type == "All" )
plotBurden(dat_plot_tmp = dat_plot_tmp , mutType = mutType)